package com.ceair.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ceair.entity.ActReDeployment;
import com.ceair.entity.request.QueryActReDeploymentReq;
import com.ceair.exception.BusinessException;
import com.ceair.mapper.ActReDeploymentMapper;
import com.ceair.service.IActReDeploymentService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 流程部署基础信息表 服务实现类
 * </p>
 *
 * @author wangbaohai
 * @since 2025-04-20
 */
@Slf4j
@Service
public class ActReDeploymentServiceImpl extends ServiceImpl<ActReDeploymentMapper, ActReDeployment> implements IActReDeploymentService {

    /**
     * 查询流程部署基础信息并分页返回结果。
     *
     * @param queryActReDeploymentReq 查询条件请求对象，包含分页信息和其他查询参数。
     *                                如果为 null 或其内部的分页信息为 null，则使用默认分页参数。
     * @return 返回一个分页对象 Page<ActReDeployment>，包含查询到的流程部署基础信息。
     * 如果查询过程中发生异常，则抛出 BusinessException。
     */
    @Override
    public Page<ActReDeployment> queryActReDeploymentWithPage(QueryActReDeploymentReq queryActReDeploymentReq) {
        // 初始化分页工具以及参数，默认为第一页，每页10条数据
        long current = 1;
        long size = 10;

        // 检查请求对象及其分页参数是否为空，并根据需要设置分页参数
        if (queryActReDeploymentReq != null && queryActReDeploymentReq.getPageReq() != null) {
            current = queryActReDeploymentReq.getPageReq().getCurrent() > 0 ?
                    queryActReDeploymentReq.getPageReq().getCurrent() : current;
            size = queryActReDeploymentReq.getPageReq().getSize() > 0 ?
                    queryActReDeploymentReq.getPageReq().getSize() : size;
        }

        // 初始化分页对象，用于存储分页查询结果
        Page<ActReDeployment> page = new Page<>(current, size);

        // 调用 mapper 进行分页查询，捕获可能的异常并记录日志
        try {
            return this.baseMapper.queryActReDeploymentWithPage(page, queryActReDeploymentReq);
        } catch (Exception e) {
            // 捕获异常并记录日志，避免系统崩溃
            log.error("查询流流程部署基础信息分页失败，请求参数: {}", queryActReDeploymentReq, e);
            throw new BusinessException("查询流程部署基础信息分页失败，请稍后重试", e);
        }
    }


}
